<?php
/**
 * 食圈
 * User: Tsang
 * Date: 2018/3/24 10:23
 *
 */

namespace app\admin\Controller;


class Circle extends Common
{


    /**
     * 食圈列表
     */
    public function index(){
        $list = db('foods_circle')->alias('fc')->join('member m','m.id = fc.user_id')->where([
            'del_time' => 0
        ])
            ->order('fc.top desc,fc.create_time desc')
            ->paginate(15);
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 食圈编辑
     */
    public function det(){
        $id = input('id/s');
        $m_fk = db('foods_circle');
        if($id > 0){
            $circle = $m_fk->where(['id' => $id])->find();
            if(!$circle)$this->error('找不到食圈');
            $this->assign('data',$circle);
        }
        if($this->request->isPost()){
            $top = input('top/s');
            $data = array(
                'top' => htmlspecialchars($top)
            );
            if($id > 0){
                if($m_fk->where(['id' => $id])->update($data) === false) $this->error('系统错误');
            }else {
                $data['create_time'] = TIMESTAMP;
                if (!$m_fk->insert($data)) $this->error('系统错误');
            }
            $this->redirect(url('Circle/index'));
        }
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function del(){
        $id = input('id/d');
        $m_fk = db('foods_circle');
        $circle = $m_fk->where(['id' => $id])->find();
        if(!$circle)$this->error('找不到食圈');
        if($m_fk->where(['id' => $id])->update(['del_time' => NOW_TIME]) === false) $this->error('删除失败');
        $this->redirect(url('Circle/index'));
    }
}